var isNewUser = false;
var hasActive = false;
var isBack = true;
var b_Url = $('#b_Url').val();
var phonestr = '';

$(function(){
	
		getMyFlower();
		checkIsNuewUser();   
	
		$('#page1 .flist').height($('#page1 .flist').width()/5);
		$('.vflist').height($('.vflist').width()/5);
		$('.btn_dh,.btn_input').height($(window).width()/286*78/2);
	
		$('#status2 a').unbind('touchstart').on('touchstart',function(){
			location.href = 'http://m.bl.com/h5-web/member/view_login.html?redirctUrl=http%3A%2F%2Fm.bl.com%2Fh5-web%2Fshop%2FviewLocation.html%3F';
		});
	
		$('.sub_tab .back').unbind('touchstart').on('touchstart',function(){
			$('.tab_title').removeClass('tab_title1').removeClass('tab_title2').removeClass('tab_title3');
			$('#infopage').animate({top:'92%'},500);
			return false;
		});
	
		$('.btnbox,.btnbox1').height($(window).width()*0.9*0.48/286*78);
	
		$('.tab_title u').click(function(){
				var idx = $(this).attr('d_index');
				if($('.tab_title').hasClass('tab_title'+idx)){
					$('.tab_title').removeClass('tab_title1').removeClass('tab_title2').removeClass('tab_title3');
					$('#infopage').animate({top:'92%'},500);
					return false;
				}
				if(!$('.tab_title').hasClass('tab_title1')&&!$('.tab_title').hasClass('tab_title2')&&!$('.tab_title').hasClass('tab_title3')){
					$('#infopage').animate({top:0},500);
				}
				$('.tab_title').removeClass('tab_title1').removeClass('tab_title2').removeClass('tab_title3');
				$('.tab_title').addClass('tab_title'+idx);
				$('.tab_body .pbody').hide();
				$('#pg_'+idx).show();
				$('.sub_tab').height($('.sub_tab').width()/383*66);
				$('.vflist').height($('.vflist').width()/5);
			});
		//$($('.tab_title u')[2]).click();
		$('.btn_join_1').click(function(){ 
				/*
				//如果是新用户 
				*/
			});
	
		$('.btn_guanzhu').click(function(){
				$('.pop_addfriend').fadeIn();
				$('.pop_addfriend').unbind('click').click(function(){
						$('.pop_addfriend').fadeOut();
					});
			});
	
		$('.sub_tab u').click(function(){
				var idx = $(this).attr('d_index');
				$('.sub_tab').removeClass('sub_tab1').removeClass('sub_tab2').addClass('sub_tab'+idx);
				$('.stab_body').hide();
				if(1==idx){
					$('#phb').show();
				}else{
					$('#log').show();
				}
			});
	
		$($('.sub_tab u')[0]).click();
	
		//获取阳光值 
		$('.btn_get_1').click(function(){
				//判断有没有关注过微信
				var url = '/bhqf/index.php/main/checkhasguanzhu';
				$.getJSON(url,function(obj){
						//没有关注 显示关注页面
						if(!obj.s){ 
							$('.btnbox').fadeOut(function(){ $('.btnbox1').fadeIn(); });
						}else{
							//如果没有发起过活动
							if(!hasActive && isBack){
								isBack = false;
								var url = '/bhqf/index.php/main/createactive'
								$.getJSON(url,function(obj){
										isBack = true;
										$('.pop_share').fadeIn();
										ShareConfig.desc = 'i百联绽放公测，百花齐放看杰伦 联心合友聚阳光';
										ShareConfig.link = b_Url+'/index.php/main/share/acid/'+obj.d;
										setShareInfo();
									});
							}else{ 
								$('.pop_share').fadeIn(); 
							}				
						}
					});
			});
	
		$('.pop_share').unbind('click').click(function(){
				$('.pop_share').fadeOut();
			}); 
			
		$('.btn_duijian_1').click(function(){
				$($('.tab_title u')[1]).click();
			});
	
		var dx, dy;
		var pg_1 = document.getElementById("pg_1");
		touch.on(pg_1, 'touchstart', function(ev){
				dx = dy = 0;
				ev.preventDefault();
			});
		touch.on(pg_1, 'drag', function(ev){ 
				dx = dx || 0;
				dy = dy || 0;		
			});
		touch.on(pg_1, 'dragend', function(ev){
				dx += ev.x;
				dy += ev.y;
				if(dy>100&&dx<40){
					$('.tab_title').removeClass('tab_title1').removeClass('tab_title2').removeClass('tab_title3');
					$('#infopage').animate({top:'92%'},500);
				}
			}); 
		var pg_2 = document.getElementById("pg_2");
		touch.on(pg_2, 'touchstart', function(ev){
				dx = dy = 0;
				ev.preventDefault();
			});
		touch.on(pg_2, 'drag', function(ev){ 
				dx = dx || 0;
				dy = dy || 0;		
			});
		touch.on(pg_2, 'dragend', function(ev){
				dx += ev.x;
				dy += ev.y;
				if(dy>100&&dx<40){
					$('.tab_title').removeClass('tab_title1').removeClass('tab_title2').removeClass('tab_title3');
					$('#infopage').animate({top:'92%'},500);
				}
			});
	
		//获取排行榜数据
		$('#phbbox').height($(window).height()-155);
		//phbbox = new iScroll('phbbox');
		getPHBList();
	
		//获取阳光值数据
		$('#lgbox').height($(window).height()-125);
		//lgbox = new iScroll('lgbox');
		getLGList();
	
		$('#btn_yhqdh').unbind('touchstart').on('touchstart',function(){
			
			if($('#btn_yhqdh').hasClass('disable')) return false;
		
				//用户是否是会员
				if(!isNewUser){
			
					dologin();
				}//如果是会员 直接领取
				else{
					//发券
					sendCoupon();
				}
			});
	});

function dologin(){
	$('#input_box').css({top:($(window).height()-$('#input_box').height())/2});
					$('#input_box,#input_mark').fadeIn(function(){
							$('#input_box .input_close').unbind('touchstart').on('touchstart',function(){
								$('#input_box,#input_mark').fadeOut();
							});
							//检测手机号码
				
							$('#getCodebtn').unbind('touchstart').on('touchstart',function(){ 
									var _dom = $(this);
									if($(_dom).hasClass('isnotback')) return false;
									var parm = {phone:$('#t_phone').val()};
									if(parm.phone.length!=11){
										alert('手机号码输入有误!');
										return false;
									} 
									$('#mloading').fadeIn();
									yzmCount = 30;
									$.post('/bhqf/index.php/main/checkphone',parm,function(obj){
											$('#mloading').fadeOut();
											//老用户未绑定  
											if(obj.resCode!='00100000'){
												console.log('Send Login Code');
												//发送登陆验证码
												isNewUser = false;
												$.post('/bhqf/index.php/main/sendlogincode',parm,function(tobj){
														$('#mloading').fadeOut();
														if(tobj.resCode=='00100000'){
															$(_dom).addClass('isnotback');
															tml = setInterval(function(){
																	yzmCount--;
																	if(yzmCount<=0){
																		clearInterval(tml);
																		$(_dom).removeClass('isnotback');
																	}
																},1000);
															//alert('登陆验证码发送成功！');
															console.log('Send Login Code OK');
															//显示登陆信息 
															$('#btn_submit_input').unbind('touchstart').on('touchstart',function(){
									
																	var loginparm = { phone:$('#t_phone').val(),smsCode:$('#t_code').val()};
																	if(loginparm.phone.length!=11){
																		alert('手机号码输入有误!');
																		return false;
																	}
																	if(loginparm.smsCode.length<1){
																		alert('请输入验证码!');
																		return false;
																	}
																	//登陆绑定
																	$('#mloading').fadeIn();
																	$.post('/bhqf/index.php/main/weixinloginandbind',loginparm,function(tobj){
																			if(tobj.resCode=='00100000'){
																				sendCoupon();
																			}else{
																				$('#mloading').fadeOut();
																				//alert(tobj.msg);
																			}
																		},'json');
																});
								
														}else{
															alert(tobj.msg);
														}
														isBak = true;
													},'json');
											}																																														
											//新用户需要注册
											else{
												//发送注册验证码
												isNewUser = true;
												console.log('Send Regster Code');
												$.post('/bhqf/index.php/main/sendregistercode',parm,function(tobj){
														$('#mloading').fadeOut();
														if(tobj.resCode=='00100000'){
															//alert('注册验证码发送成功！');
															//显示注册信息
															$(_dom).addClass('isnotback');
															tml = setInterval(function(){
																	yzmCount--;
																	if(yzmCount<=0){
																		clearInterval(tml);
																		$(_dom).removeClass('isnotback');
																	}
																},1000);   
															$('#btn_submit_input').unbind('touchstart').on('touchstart',function(){
																	var loginparm = { phone:$('#t_phone').val(),smsCode:$('#t_code').val()};
																	if(loginparm.phone.length!=11){
																		alert('手机号码输入有误!');
																		return false;
																	}
																	if(loginparm.smsCode.length<1){
																		alert('请输入验证码!');
																		return false;
																	}
																	//注册绑定
																	$('#mloading').fadeIn();
																	$.post('/bhqf/index.php/main/weixinregisterandbind',loginparm,function(tobj){
																			if(tobj.resCode=='00100000'){ 
																				sendCoupon(); 
																			}else{
																				$('#mloading').fadeOut();
																				//alert(tobj.msg);
																			}
																		},'json');
																});
								
														}else{
															alert(tobj.msg);
														}
														isBak = true;
													},'json');
											}																																													
											isBak = true;
										},'json'); 
								});
						});
}

//发券
function sendCoupon(){ 
	$.post('/bhqf/index.php/main/sendCoupon',{},function(tobj){
		$('#mloading').fadeOut();
		if(tobj.resCode=='00100000'){
			$('#status1').hide();
			$('#status2').show();
			$('#input_box,#input_mark').fadeOut();
		}else{
			//alert(tobj.msg); 
			dologin();
		}
	},'json');
}

function getPHBList(){
	var url ='/bhqf/index.php/main/getPHBList';
	$.getJSON(url,function(obj){
			if(!obj.d.list){
				$('#phbbox').html('<center>您还未参与活动或者暂无可见数据<br />请在首页点击“我要参与活动”按钮查看数据<center>');
			}else{
				var html = '<dd>'
				+'	<span><b>'+obj.d.my.user.nickname+'</b><em style="background-image: url('+obj.d.my.user.headimgurl+');"></em></span>'
				+'	<span><b>阳光值：<strong>'+obj.d.my.f_count+'</strong></b></span>'
				+'	<span><b>排名：<strong>'+obj.d.my.paiming+'</strong></b></span>'
				+'</dd>';
				$.each(obj.d.list,function(i,d){
					var thtml = '<dd class="ll">'
						+'	<strong>'+ (i<9?'0'+(i+1):(i+1)) +'</strong><b>阳光值'+d.f_count+'</b><em style="background-image: url('+d.user.headimgurl+');"></em><u>'+d.user.nickname+'</u>'
						+'</dd>';
						html += thtml;
				});
				$('#phbbox dl').append(html);
				$('#haicha').text(obj.d.my.paiming-24>0?obj.d.my.paiming-24:0); 
				$('#phb_empty').remove();
			}
		});
}
function getLGList(){ 
	var url = '/bhqf/index.php/main/getLGList';
	$.getJSON(url,function(obj){
			if(obj.d.length<1){
				$('#lgbox').html('<center>您还未参与活动或者暂无可见数据<br />请在首页点击“我要参与活动”按钮查看数据<center>');
			}else{
				var html = '';
				$.each(obj.d,function(i,d){
					var thtml = '<dd>'
						+'	<em style="background-image: url('+d.user.headimgurl+');"></em>'
						+'	<p>'+d.user.nickname+' '+d.timestr+'<br /><b>成功补给阳光</b></p>'
						+'	</dd>';
						html += thtml;
				}); 
				$('#lgbox dl').append(html); 
				$('#lg_empty').remove();
			}
		}); 
}

function checkIsNuewUser(){
	var url = '/bhqf/index.php/main/loginby3rdopenid';
	$.getJSON(url,function(obj){
			isNewUser = obj.isNewUser;
			phonestr = obj.phone;
			$('#t_phone').val(phonestr);
	}); 
}

//获取我发起活动的数据
function getMyFlower(){
	
	var url = '/bhqf/index.php/main/getMyFlower';
	$.getJSON(url,function(obj){
			if(obj.s){
				hasActive = true;
				ob = obj.d;
				showFlower([ob.f1,ob.f2,ob.f3,ob.f4,ob.f5]);
				//已获得券
				$('#jp_empty').hide();
				if(ob.status==2){
					$('#status2').show();
					$('#status1').hide();
				}else{
					$('#status1').show();
					$('#status2').hide();
					$('.vflist').height($('.vflist').width()/5);
				}
				ShareConfig.desc = 'i百联绽放公测，百花齐放看杰伦 联心合友聚阳光';
				ShareConfig.link = b_Url+'index.php/main/share/acid/'+ob.id;
				setShareInfo();
			}else{
				$('#status2').hide();
				$('#status1').hide();
				$('#jp_empty').show();
			}
		});
}

function showFlower(data){
	var ct = 0;
	$.each(data,function(i,d){
			if(d==1){
				//设置首页的花色
				$('#indexfshow u.u.u'+(i+1)).addClass('open');
				//设置奖品页面的花色
				$('#prizefshow u.u.u'+(i+1)).addClass('open');
				ct++;
			}
		});
	if(ct==5){
		$('#btn_yhqdh').removeClass('disable');
	}
	
	$('#tf_count').text(ct);
	
}